package com.atguigu.gmall.order.config;

import com.atguigu.gmall.rabbit.config.MqConst;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.CustomExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建取消订单延迟关闭订单需要交换机跟队列完成绑定
 */
@Configuration
public class CancelOrderBindingConfig {

    @Bean
    public Queue cancelOrderQueue() {
        // 第一个参数是创建的queue的名字，第二个参数是是否支持持久化
        return new Queue(MqConst.QUEUE_ORDER_CANCEL, true);
    }

    /**
     * 声明延迟类型交换机
     * 前提：必须要保证MQ配置开启延迟插件
     * @return
     */
    @Bean
    public CustomExchange cancelOrderExchange() {
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("x-delayed-type", "direct");
        return new CustomExchange(MqConst.EXCHANGE_DIRECT_ORDER_CANCEL, "x-delayed-message", true, false, args);
    }

    @Bean
    public Binding delayBbinding1() {
        return BindingBuilder.bind(cancelOrderQueue()).to(cancelOrderExchange()).with(MqConst.ROUTING_ORDER_CANCEL).noargs();
    }
}
